<template>
  <div class="app-container">
    <div class="filter-container">
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">
        筛选
      </el-button>
      <el-button class="filter-item" style="margin-left: 10px;" type="success" icon="el-icon-edit" @click="handleCreate">
        发布
      </el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      max-height="690"
      tooltip-effect="light"
      highlight-current-row
      style="width: 100%;"
      :default-sort="{prop: 'noticeId', order: null}"
      @sort-change="sortChange"
    >
      <el-table-column label="ID" align="center" width="80" fixed prop="noticeId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.noticeId }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消息标题" align="center" show-overflow-tooltip width="300px">
        <template slot-scope="{row}">
          <span>{{ row.noticeTitle }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消息简介" align="center" show-overflow-tooltip width="400px">
        <template slot-scope="{row}">
          <span>{{ row.noticeDesc }}</span>
        </template>
      </el-table-column>
      <el-table-column label="接收用户" align="center" prop="userId" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.realName }}</span>
        </template>
      </el-table-column>
      <el-table-column label="消息类型" align="center" prop="noticeType" sortable="custom">
        <template slot-scope="{row}">
          <el-tag size="mini" :type="noticeTypeMap[row.noticeType].type"> {{ noticeTypeMap[row.noticeType].label }} </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="noticeRead" sortable="custom">
        <template slot-scope="{row}">
          <el-tag v-if="row.noticeRead===0" size="mini" type="danger" effect="dark"> 未读 </el-tag>
          <el-tag v-else size="mini" type="info" effect="dark"> 已读 </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="发送时间" align="center" prop="createTime" sortable="custom">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="260px" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="handleDetail(row.noticeId)">
            详情
          </el-button>
          <el-button type="success" size="mini" @click="handleCopy(row.noticeId)">
            复制
          </el-button>
          <el-button size="mini" type="danger" @click="handleDelete(row.noticeId)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total>0" :total="total" :page-num.sync="listQuery.pageNum" :page-size.sync="listQuery.pageSize" @pagination="showNoticeList" />

    <el-dialog title="消息详情" :visible.sync="dialogDetailVisible" width="460px" :show-close="false" style="padding-bottom: 0">
      <h2>{{ detailTemp.noticeTitle }}</h2>
      <el-card shadow="hover">
        <div v-html="detailTemp.noticeContent" />
      </el-card>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogDetailVisible = false">
          关闭
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { showNoticeList, deleteNotice, getNotice } from '@/api/notice'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination'

export default {
  name: 'Notice',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      tableKey: 0,
      list: [],
      total: 0,
      listLoading: true,
      brandList: [],
      listQuery: {
        pageNum: 1,
        pageSize: 20,
        departmentId: ''
      },
      detailTemp: {
        noticeTitle: '',
        noticeContent: ''
      },
      noticeTypeMap: {
        0: { label: '公告说明', type: 'info' },
        1: { label: '待领取卡券', type: 'success' },
        2: { label: '过期卡券', type: 'danger' }
      },
      dialogDetailVisible: false
    }
  },
  created() {
    this.showNoticeList()
  },
  methods: {
    showNoticeList() {
      this.listLoading = true
      this.listQuery.departmentId = this.$store.getters.tmpDepartmentId
      showNoticeList(this.listQuery).then(response => {
        this.list = response.data.list
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.showNoticeList()
    },
    sortChange(data) {
      const { prop, order } = data
      this.listQuery.sortProp = prop
      if (order === 'ascending') {
        this.listQuery.sortOrder = '1'
      } else {
        this.listQuery.sortOrder = '-1'
      }
      this.handleFilter()
    },
    handleCreate() {
      this.$router.push({
        name: 'NoticeEdit',
        params: { id: 0 }
      })
    },
    handleCopy(noticeId) {
      this.$router.push({
        name: 'NoticeEdit',
        params: { id: noticeId }
      })
    },
    handleDetail(noticeId) {
      this.dialogDetailVisible = true
      getNotice(noticeId).then(response => {
        this.detailTemp = response.data
      })
    },
    handleDelete(noticeId) {
      this.$confirm('此删除操作不可逆, 是否继续?', '提示', {
        confirmButtonText: '确认删除',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteNotice(noticeId).then(() => {
          this.showNoticeList()
          this.dialogFormVisible = false
          this.$notify({
            title: 'Success',
            message: '删除系统消息成功',
            type: 'success',
            duration: 2000
          })
        })
      })
    }
  }
}
</script>
